<?php

class Studentgrade extends Controller
{
	function Studentgrade()
	{
		parent::Controller();
		$this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('email');
	}
	
	function index()
	{
	}
		
	
	function showgrade()
	{
		$imie = $this->input->post('imie');
		$nazwisko = $this->input->post('nazwisko');
		
		$query = "SELECT name FROM subjects";
		$przedmioty = $this->db->query($query);
		
		$query = "SELECT su.name, COUNT(g.id) AS liczba FROM subjects su, descriptions d, grades g, students s WHERE su.id = d.subject_id AND g.description_id = d.id 
				AND s.id = g.student_id AND s.firstname = '" . $imie . "' AND s.secondname = '" . $nazwisko . "' GROUP BY su.name ORDER BY su.name";
		$liczbaocenzprzedmiotu = $this->db->query($query);
		
		$query = "SELECT su.name AS przedmiot, d.description AS opis, g.date AS data, g.grade AS ocena, d.weight AS waga FROM subjects su, descriptions d, grades g, students s 
					WHERE su.id = d.subject_id AND g.description_id = d.id AND s.id = g.student_id AND s.firstname = '" . $imie . "' AND s.secondname = '" 
					. $nazwisko . "'ORDER BY su.name, d.description";
		$oceny = $this->db->query($query);			
		
		$query = "SELECT name AS klasa FROM students s, classes c WHERE s.classe_id = c.id AND s.firstname = '" . $imie . "' AND s.secondname = '" . $nazwisko ."'";
		$query = $this->db->query($query);
		
		foreach ($query->result() as $row)
    	{ 			
   			$klasa = $row -> klasa;
    	}
		
    	$query = "SELECT t.id As id, t.firstname AS imie, t.secondname AS nazwisko, s.name AS przedmiot FROM classes c, subjects_teachers st, teachers t, subjects s WHERE c.id = st.classe_id AND s.id = st.subject_id AND 
					t.id = st.teacher_id AND c.name = '" . $klasa . "' ORDER BY przedmiot";
		$przedmiotynauczyciele = $this->db->query($query); 
    	
		$tablica = array('imie' => $imie, 'nazwisko' => $nazwisko, 'przedmioty' => $przedmioty, 'liczbaocenzprzedmiotu' => $liczbaocenzprzedmiotu, 'oceny' => $oceny,
						'klasa' => $klasa, 'przedmiotynauczyciele' => $przedmiotynauczyciele);
		$this->load->view("studentgrades_view", $tablica);
	}
	
	
	function showgradee($imie, $nazwisko)
	{
		
		if(NULL ==  getenv('HTTP_REFERER'))
		{
			echo "NASTĄPIŁA PRÓBA WYŁUDZENIA INFORMACJI !";
		}
		else
		{
			$query = "SELECT name FROM subjects";
			$przedmioty = $this->db->query($query);
		
			$query = "SELECT su.name, COUNT(g.id) AS liczba FROM subjects su, descriptions d, grades g, students s WHERE su.id = d.subject_id AND g.description_id = d.id 
					AND s.id = g.student_id AND s.firstname = '" . $imie . "' AND s.secondname = '" . $nazwisko . "' GROUP BY su.name ORDER BY su.name";
			$liczbaocenzprzedmiotu = $this->db->query($query);
		
			$query = "SELECT su.name AS przedmiot, d.description AS opis, g.date AS data, g.grade AS ocena, d.weight AS waga FROM subjects su, descriptions d, grades g, students s 
						WHERE su.id = d.subject_id AND g.description_id = d.id AND s.id = g.student_id AND s.firstname = '" . $imie . "' AND s.secondname = '" 
						. $nazwisko . "'ORDER BY su.name, d.description";
			$oceny = $this->db->query($query);			
		
			$query = "SELECT name AS klasa FROM students s, classes c WHERE s.classe_id = c.id AND s.firstname = '" . $imie . "' AND s.secondname = '" . $nazwisko ."'";
			$query = $this->db->query($query);
		
			foreach ($query->result() as $row)
    		{ 			
   				$klasa = $row -> klasa;
    		}
		
    		$query = "SELECT t.id As id, t.firstname AS imie, t.secondname AS nazwisko, s.name AS przedmiot FROM classes c, subjects_teachers st, teachers t, subjects s WHERE c.id = st.classe_id AND s.id = st.subject_id AND 
					t.id = st.teacher_id AND c.name = '" . $klasa . "' ORDER BY przedmiot";
			$przedmiotynauczyciele = $this->db->query($query); 
    	
			$tablica = array('imie' => $imie, 'nazwisko' => $nazwisko, 'przedmioty' => $przedmioty, 'liczbaocenzprzedmiotu' => $liczbaocenzprzedmiotu, 'oceny' => $oceny,
						'klasa' => $klasa, 'przedmiotynauczyciele' => $przedmiotynauczyciele);
			$this->load->view("studentgrades_view", $tablica);
		}
	}
	
	
	
	function showattendance()
	{
		$imie = $this->input->post('imie');
		$nazwisko = $this->input->post('nazwisko');
		
		
		$query = "SELECT name FROM subjects";
		$przedmioty = $this->db->query($query);
		
		$query = "SELECT su.name AS przedmiot, a.dayy AS dzien, a.monthh AS miesiac, a.yearr AS rok, a.lesson AS lekcja, a.attendance AS obecnosc FROM students s, 
				attendances a, subjects su WHERE s.id = a.student_id AND su.id = a.subject_id AND s.firstname = '" . $imie . "' AND s.secondname = '" . $nazwisko . "' 
				ORDER BY przedmiot, rok, dzien, miesiac, lekcja";
		$obecnosci = $this->db->query($query);
		
		$query = "SELECT name AS klasa FROM students s, classes c WHERE s.classe_id = c.id AND s.firstname = '" . $imie . "' AND s.secondname = '" . $nazwisko ."'";
		$query = $this->db->query($query);
		
		foreach ($query->result() as $row)
    	{ 			
	    	$klasa = $row -> klasa;
    	}
    	
    	$query = "SELECT t.id As id, t.firstname AS imie, t.secondname AS nazwisko, s.name AS przedmiot FROM classes c, subjects_teachers st, teachers t, subjects s WHERE c.id = st.classe_id AND s.id = st.subject_id AND 
					t.id = st.teacher_id AND c.name = '" . $klasa . "' ORDER BY przedmiot";
		$przedmiotynauczyciele = $this->db->query($query); 
		
    	
		$tablica = array('imie' => $imie, 'nazwisko' => $nazwisko, 'przedmioty' => $przedmioty, 'obecnosci' => $obecnosci, 'klasa' => $klasa, 
					'przedmiotynauczyciele' => $przedmiotynauczyciele);
		$this->load->view("studentattendances_view", $tablica);
	}
}

?>